<?php
class Bap_model extends MY_Model {
	
    function Bap_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "bap";
        $this->table_fields = array('bap_id','bap_tgl','bap_pembuat_kary_id','bap_pembuat_kary_jbt_id',
        	'bap_pembuat_keterangan','bap_status','perb_id', 'kary_id','kary_jbt_id');
    }
    
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
		if ($search_key){
			if (!is_array($search_key)) $search_key = array($search_key);
			
			$qq = '';
			foreach($search_key as $kk){
				foreach($this->table_fields as $ff){
					$qq .= ($qq? " OR " : "").$this->table_name.".$ff LIKE ". $this->db->escape("%$kk%"); 
				}
			}
			$this->where("($qq)");
		}
		
		if ($where) $this->where($where);
		
    	if ($orderby) $this->order_by($orderby);
		
		return $this->get($limit, $offset);
    }
        
    function select_by_id($id){
    	return $query = $this->db->get_where($this->table_name, array('bap_id' => $id));
    }
    
    function select_by_perbID($perb_id){
    	return $this->get_where(array('perb_id' => $perb_id));
    }
    
	function get_last_entry(){
    	$this->select_max('bap_id');
    	return $this->get()->row();
    }
    
    /**
     * cek apakah semua saksi telah menandatangani
     * @param $bapID
     */
	function is_signed_by_witnesses($bapID){
		$this->join('bap_sign','bap_sign.bap_id='.$this->table_name.'.bap_id');
    	$res = $this->get_where(array($this->table_name.'.bap_id'=>$bapID, 'bap_sign_type'=>'WT'));
    	$sign = true;
    	foreach($res->result_array() as $row){
    		$sign  &= $row['bap_sign_signing'] != NULL;
    		if (!$sign) return false; 
    	}
    	return true;
    }
    
    /**
     * cek apakah pelaku telah menandatangani
     * @param $bapID
     */
    function is_signed_by_actor($bapID){
    	$this->join('bap_sign','bap_sign.bap_id='.$this->table_name.'.bap_id');
    	$res = $this->get_where(array($this->table_name.'.bap_id'=>$bapID, 'bap_sign_type'=>'AC'));
    	$sign = true;
    	foreach($res->result_array() as $row){
    		$sign  &= $row['bap_sign_signing'] != NULL;
    		if (!$sign) return false; 
    	}
    	return true;
    }
    
    function getSignedBAP($targetUsr, $key=false, $where="", $limit=10, $offset=0,$orderby=""){
    	return $this->select_last_entry($key, 
    		"kary_id='$targetUsr' AND bap_status='SI'". ($where? "AND $where" : ""), 
    		$limit, 
    		$offset, 
    		$orderby);
    }
    
    function getActiveBAP($targetUsr, $key=false, $where="", $limit=10, $offset=0,$orderby=""){
    	return $this->select_last_entry($key, 
    		"kary_id='$targetUsr' AND bap_status IN ('SA','CR') ". ($where? "AND $where" : ""), 
    		$limit, 
    		$offset, 
    		$orderby);
    }

	function getCounterBAP($targetUsr, $key=false, $where="", $limit=10, $offset=0,$orderby=""){
		$where2 = 
		"(bap_pembuat_kary_id IN (
		    SELECT x.kary_id FROM karyawan_history_jabatan as x WHERE x.jbt_id IN (
		        SELECT xx.jbt_id from jabatan as xx where xx.jbt_supervisi IN (
		            SELECT xxx.jbt_id FROM karyawan_history_jabatan as xxx
		            JOIN jabatan as yyy ON yyy.jbt_id = xxx.jbt_id
		            WHERE xxx.kary_id='$targetUsr' 
		            AND NOW() BETWEEN xxx.kary_jbt_tgl_menjabat AND xxx.kary_jbt_tgl_berhenti
		        )
		    )
		) OR bap_pembuat_kary_id='$targetUsr'
		)
   		AND bap_status='CO'";
		return $this->select_last_entry($key, 
    		"$where2". ($where? "AND $where" : ""), 
    		$limit, 
    		$offset, 
    		$orderby);
	}
	
	function getCounterRejectedBAP($targetUsr, $key=false, $where="", $limit=10, $offset=0,$orderby=""){
		return $this->select_last_entry($key, 
    		"(kary_id='$targetUsr' OR bap_pembuat_kary_id='$targetUsr')AND bap_status='CR'". ($where? "AND $where" : ""), 
    		$limit, 
    		$offset, 
    		$orderby);
	}
	
	function getCounterApprovedBAP($targetUsr, $key=false, $where="", $limit=10, $offset=0,$orderby=""){
		return $this->select_last_entry($key, 
    		"(kary_id='$targetUsr' OR bap_pembuat_kary_id='$targetUsr') AND bap_status='CA'". ($where? "AND $where" : ""), 
    		$limit, 
    		$offset, 
    		$orderby);
	}
}